home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / networking / pgpuam / sources / pgpuamdialogtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  8.6 KB  |  338 lines

  1. //------------------------------------------------------------------------------------
  2. #pragma mark Includes
  3. //------------------------------------------------------------------------------------
  4. //
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include <string.h>
  8. #include <console.h>
  9. #include <sioux.h>
  10.  
  11. #include <Errors.h>
  12. #include <String.h>
  13. #include <A4Stuff.h>
  14. #include <Resources.h>
  15. #include <MixedMode.h>
  16. #include <CodeFragments.h>
  17. #include <Gestalt.h>
  18. #include <PLStringFuncs.h>
  19. #include "ClientUAM.h" 
  20. #include "AFPPackets.h"
  21.  
  22. #include <stdio.h>
  23.  
  24. #include "TPGPkey.h"
  25. #include "TPGPException.h"
  26. #include "TMacException.h"
  27. #include "TASIPPGPkey.h"
  28. #include "ASIPChallenge.h"
  29. #include "PGPUAMclient.h"
  30. #include "PGPUAMclientLoginDialog.h"
  31. #include "PGPUAMclientProtocol.h"
  32. #include "PGPUAMmsgFormat.h"
  33. #include "PGPUAMclient.h"
  34.  
  35. #include <Appearance.h>
  36.  
  37. pascal     OSStatus     __pgpuam_client (UAMArgs *theArgs);
  38.  
  39.  
  40. // ---------------------------------------------------------------------------
  41. #pragma mark Globals
  42. // ---------------------------------------------------------------------------
  43.  
  44.      static TASIPPGPkey         gsTheServerKey;
  45.      static PGPKeyRef         gsTheClientKeyRef    = kInvalidPGPKeyRef;
  46.     static Str255               gsCounterChallengeString;
  47.      
  48.      static AFPClientInfo    gClientInfo;
  49.  
  50.      
  51.  
  52.     pascal OSStatus  simulateOpenSession(OTAddress *addr, const char *endpointString, UAMMessagePtr message);
  53.     pascal OSStatus  simulateSendRequest(UAMMessagePtr message);
  54.     pascal OSStatus  simulateCloseSession(short sessRefNum);
  55.     pascal OSStatus  simulateGetClientInfo (short infoType,ClientInfo    **infoPtr);         
  56.     pascal void      simulateEventCallback (EventRecord *theEvent);         
  57.     
  58.  
  59. pascal OSStatus  simulateOpenSession(OTAddress *addr, const char *endpointString, UAMMessagePtr message)
  60. {
  61.     PGPError            err            = kPGPError_NoErr;
  62.     PGPFilterRef         theFilter     = kInvalidPGPFilterRef;
  63.     PGPKeySetRef        newKeySet     = NULL;
  64.     PGPKeySetRef        singleSet     = NULL;
  65.     PGPKeyRef             theKeyRef    = kInvalidPGPKeyRef;
  66.     PGPKeyIterRef         theIterator = kInvalidPGPKeyIterRef;
  67.     PGPKeyListRef         theKeyListRef = kInvalidPGPKeyListRef;
  68.     PGPUInt32             numKeys;
  69.     PGPSize                bufLen;
  70.     TPGPkey                theKey;
  71.     
  72.     StringPtr             p;
  73.     StringPtr             challengeString;
  74.     
  75.     Str255                   userName;
  76.     Str255                   counterChallengeString;
  77.     Str255                 fpString;
  78.     OSStatus            status = kFPAccessDenied;
  79.     
  80.     p = message->cmdBuffer + message->cmdBuffer[1] + 2;
  81.      p = p + p[0] +1;
  82.  
  83.     PLstrcpy(userName, p);
  84.     p2cstr(userName);
  85.     
  86.      challengeString = p + p[0] +1;
  87.      
  88.     // Find key in database
  89.          
  90.     ThrowIfPGPErr( PGPNewUserIDNameFilter(TPGPkey::fgContext, (char*) userName , kPGPMatchEqual, &theFilter));
  91.     ThrowIfPGPErr( PGPFilterKeySet(TPGPkey::fgPGPKeySetRef , theFilter, &newKeySet));
  92.     PGPFreeFilter(theFilter);
  93.      ThrowIfPGPErr( PGPCountKeys(newKeySet, &numKeys));
  94.     if(numKeys > 0) 
  95.     {    
  96.         
  97.         err = PGPOrderKeySet(newKeySet,kPGPAnyOrdering, &theKeyListRef);
  98.         err = PGPNewKeyIter( theKeyListRef, &theIterator );
  99.         
  100.         while( (err = PGPKeyIterNext( theIterator,&theKeyRef )) == kPGPError_NoErr)
  101.         {
  102.             Boolean canSign;
  103.             PGPGetKeyBoolean (theKeyRef, kPGPKeyPropCanSign,  &canSign);
  104.             if(canSign) break;
  105.         }
  106.          
  107.          
  108.          ThrowIfPGPErr(err);
  109.          gsTheClientKeyRef = theKeyRef;
  110.          
  111.         PGPGetKeyPropertyBuffer( theKeyRef, kPGPKeyPropFingerprint, sizeof( fpString ) - 1, &fpString[1], &bufLen);
  112.         fpString[0] = (bufLen & 0xFF);
  113.             
  114.         PGPFreeKeyIter(theIterator );
  115.         PGPFreeKeyList(theKeyListRef );
  116.         PGPFreeKeySet(newKeySet );
  117.  
  118.         ReplyToChallenge(&gsTheServerKey, "foo", challengeString, gsCounterChallengeString);
  119.  
  120.         p = (StringPtr) FormatLoginResp( message->replyBuffer,  gsCounterChallengeString, fpString);
  121.                         
  122.         message->replyBufferSize =  p - message->replyBuffer;
  123.         status = kFPAuthContinue;
  124.     }
  125.         
  126.         
  127.     if(message->completion)
  128.         (message->completion)( message->contextPtr, status );
  129.     
  130.     return noErr;
  131. }
  132.  
  133. pascal OSStatus  simulateSendRequest(UAMMessagePtr message)
  134. {
  135.  
  136.     PUAM_LOGIN_CONT_CMD    cmd;
  137.     OSStatus err = kFPAccessDenied;
  138.     
  139.     if( message->cmdBuffer[0]  == kFPLoginCont) 
  140.     {
  141.         message->cmdBuffer = message->cmdBuffer + 4;
  142.         // parse the reply
  143.     
  144.          ParseLoginContinueCmd(message->cmdBuffer, &message->cmdBufferSize, &cmd);
  145.         
  146.         if(gsTheClientKeyRef)
  147.         {
  148.             TPGPkey theKey;
  149.             theKey.Initialize(gsTheClientKeyRef);
  150.             
  151.             if( VerifyCounterChallenge(&theKey, gsCounterChallengeString, cmd.SigPString) )
  152.                 err = noErr;
  153.         }
  154.     }
  155.     
  156.      message->replyBufferSize = 0;
  157.      
  158.     if(message->completion)
  159.         (message->completion)( message->contextPtr, err);
  160.  
  161.     return noErr;
  162. }
  163.  
  164.  
  165. pascal OSStatus  simulateCloseSession(short sessRefNum)
  166. {
  167.     return noErr;
  168.  
  169. }
  170.   
  171. pascal OSStatus   simulateGetClientInfo (short infoType,ClientInfo    **infoPtr)          
  172. {
  173.  
  174.     if (kAFPClientInfo != infoType) return -1;
  175.     *infoPtr = (ClientInfo*) & gClientInfo;
  176.     return noErr;
  177.  
  178. }
  179.  
  180.   
  181. pascal void  simulateEventCallback (EventRecord *theEvent)      
  182. {
  183.  
  184. }
  185.  
  186.   
  187. static pascal OSErr InitMac (void);
  188. static pascal OSErr InitMac (void)
  189. {
  190.     MaxApplZone ( );
  191.     InitGraf (&(qd.thePort));
  192.     InitFonts ( );
  193.     InitWindows ( );
  194.     InitMenus ( );
  195.     TEInit ( );
  196.     InitDialogs (nil);
  197.  
  198.     return noErr;
  199. }
  200.  
  201. #pragma mark -
  202.  
  203.  
  204.  
  205. //------------------------------------------------------------------------------------
  206. void main(void)
  207. //------------------------------------------------------------------------------------
  208. //
  209. //
  210. {
  211. // Initialize allocatable storage to  kInvalidPGP..
  212.     UAMArgs                    theArgs;
  213.      ClientUAMCallbackRec    theCallbacks;
  214.       OSStatus                err = -1;        
  215.       Str63                    SavedUserName;
  216.  
  217.  #if __MWERKS__
  218.     extern tSIOUXSettings    SIOUXSettings;
  219.     SIOUXSettings.asktosaveonclose = false;
  220.     SIOUXSettings.autocloseonquit    = true;
  221.     SIOUXSettings.rows = 40;
  222.     
  223. #endif
  224.     
  225.       InitMac();
  226.  
  227. // set up simulated callbacks..
  228.     theArgs.callbacks = &theCallbacks;
  229.      theCallbacks.OpenSessionUPP     =  NewRoutineDescriptor((ProcPtr) simulateOpenSession, kOpenSessionProcInfo, kPowerPCISA);
  230.      theCallbacks.SendRequestUPP      =  NewRoutineDescriptor((ProcPtr) simulateSendRequest, kSendRequestProcInfo, kPowerPCISA);
  231.      theCallbacks.CloseSessionUPP      =  NewRoutineDescriptor((ProcPtr) simulateCloseSession, kCloseSessionProcInfo, kPowerPCISA);
  232.     theCallbacks.GetClientInfoUPP      =  NewRoutineDescriptor((ProcPtr) simulateGetClientInfo, kGetClientInfoProcInfo, kPowerPCISA);
  233.     theCallbacks.EventCallbackUPP      =  NewRoutineDescriptor((ProcPtr) simulateEventCallback, kEventCallbackProcInfo, kPowerPCISA);
  234.     
  235.      gClientInfo.fInfoType = kAFPClientInfo;
  236.      gClientInfo.fVersion = 9;
  237.      gClientInfo.fNumAFPVersions = 1;
  238.      gClientInfo.fAFPVersionStrs = nil;
  239.  
  240.     { 
  241.         StringHandle name;
  242.         name = GetString(-16096);
  243.         if(name) 
  244.         {
  245.              PLstrcpy( gClientInfo.fDefaultUserName, *name );
  246.          }
  247.         else 
  248.             PLstrcpy( gClientInfo.fDefaultUserName, "\pfoo");
  249.      }
  250.  
  251.       gAFPSrvrSig =  (UInt8 *) "\xb4\x37\xd4\x87\xa2\x63\xb4\x02\x65\x9b\x79\xb9\xC5\x21\xa2\x82";
  252.  
  253.  
  254.     try
  255.     {
  256.        // open the keyring    
  257.         TPGPkey::Initialize();
  258.         TPGPkey::OpenKeyDefaultRing();
  259.     
  260.     // pickup the server key
  261.         gsTheServerKey.Initialize(gAFPSrvrSig);
  262.  
  263.  
  264.         theArgs.Opt.open.srvrInfo = (AFPSrvrInfo*) "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14When Diplomacy Fails\0"
  265.                                                     "\x00\x22"  
  266.                                                     "\xb4\x37\xd4\x87\xa2\x63\xb4\x02\x65\x9b\x79\xb9\xC5\x21\xa2\x82";
  267.         theArgs.command    = kUAMOpen;
  268.         err =  __pgpuam_client ( &theArgs );
  269.          
  270.         if( err == noErr) 
  271.         {
  272.         
  273.             SavedUserName[0] =  '\0';
  274.              theArgs.Opt.auth.userName =  SavedUserName;
  275.             theArgs.command    = kUAMLogin;
  276.             err = __pgpuam_client ( &theArgs );
  277.         }
  278.     }
  279.     
  280.     
  281.     
  282. // handle Mac errors
  283.     catch (TMacException &ex)
  284.     {
  285.         char         errorBuf[256];
  286.         SInt16             itemHit;
  287.     
  288.         err = theArgs.result = ex.GetExceptionErr(); 
  289.          sprintf(errorBuf, "PGPUAM SHELL Error: %d, %s, File: %s, Line: %d",  
  290.                              ex.GetExceptionErr(), ex.GetExceptionMessage(),  ex.GetExceptionFile(),  ex.GetExceptionLine());
  291.          StandardAlert(kAlertStopAlert,(UInt8 *)errorBuf, nil, nil , &itemHit);
  292.  
  293.          if(err == noErr) err = userCanceledErr;
  294.     }
  295.  
  296.  
  297. // handle PGP errors
  298.     catch (TPGPException &ex)
  299.     {
  300.         char         errorBuf[256];
  301.         char         explanationBuf[256];
  302.         SInt16         itemHit;
  303.          PGPGetErrorString ( ex.GetExceptionErr(), sizeof(explanationBuf), explanationBuf);
  304.         
  305.         sprintf(errorBuf, "PGPUAM SHELL Error: %d, %s, File: %s, Line: %d",  
  306.                              ex.GetExceptionErr(), ex.GetExceptionMessage(),  ex.GetExceptionFile(),  ex.GetExceptionLine());
  307.     
  308.         c2pstr(explanationBuf);
  309.         c2pstr(errorBuf);
  310.          StandardAlert(kAlertStopAlert,(UInt8 *)errorBuf, (UInt8 *)explanationBuf, nil , &itemHit);
  311.         err = theArgs.result = userCanceledErr; 
  312.     }
  313.     
  314.     catch(...)
  315.     {
  316.     }
  317.     
  318.  
  319.     theArgs.command    = kUAMClose;
  320.     __pgpuam_client ( &theArgs );
  321.  
  322.  
  323.     TPGPkey::CloseKeyRing();
  324.     
  325.      DisposeRoutineDescriptor(theCallbacks.OpenSessionUPP);
  326.      DisposeRoutineDescriptor(theCallbacks.SendRequestUPP);
  327.  
  328.     if((err != noErr)  && (err != userCanceledErr))
  329.         {
  330.             SInt16  itemHit;
  331.              StandardAlert(kAlertStopAlert,"\pAuthentication Failed!",  nil, nil , &itemHit);
  332.  
  333.         }
  334.  
  335.  }
  336.  
  337.  
  338.